﻿package com.eve.eplus.common.tools;

import java.io.File;

import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Environment;
import android.os.IBinder;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.eve.eplus.common.app.Constants;


/**
 * 设备信息获取工具
 * @author Norsie
 * @created 12-11-29
 */
public class ComDeviceTools {
    /**
     * 判断是否已连接到网络.
     * 
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 检查网络接连类型.
     * 
     * @param context
     * @return SysConstants.NETWORK_TYPE_NONE: 无网络连接;
     *         SysConstants.NETWORK_TYPE_WIFI: 通过WIFI连接网络;
     *         SysConstants.NETWORK_TYPE_WAP: 通过GPRS连接网络.
     */
    public static int checkNetWorkType(Context context) {
        if (isAirplaneModeOn(context)) {
            return Constants.NETWORK_TYPE_NONE;
        }
        State wifiState = null;  
        State mobileState = null;
        int subType=-1;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  

        if (wifiState != null && mobileState != null  
                && State.CONNECTED != wifiState  
                && State.CONNECTED == mobileState) {  
            Log.i("NetInfo", "当前网络类型:蜂窝网络");
            subType=cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getSubtype();

            if(subType==TelephonyManager.NETWORK_TYPE_1xRTT ||
                    subType==TelephonyManager.NETWORK_TYPE_CDMA ||
                    subType==TelephonyManager.NETWORK_TYPE_EDGE ||
                    subType==TelephonyManager.NETWORK_TYPE_GPRS ||
                    subType==TelephonyManager.NETWORK_TYPE_IDEN
                    ){
                //低速网络,速度在200K以下
                Log.i("NetInfo", "当前网络为:低速网络");
                return Constants.NETWORK_TYPE_LOW;
            }
            else if (subType==TelephonyManager.NETWORK_TYPE_UNKNOWN)
            {//无网络
                Log.i("NetInfo", "当前网络为:无网络");
                return Constants.NETWORK_TYPE_NONE;
            }
            else{
                //高速网络,速度在200K以上
                Log.i("NetInfo", "当前网络为:高速网络");
                return Constants.NETWORK_TYPE_HIGH;
            }

        } else if (wifiState != null && mobileState != null  
                && State.CONNECTED != wifiState  
                && State.CONNECTED != mobileState) {  
            Log.i("NetInfo", "当前网络类型:无网络");
            return Constants.NETWORK_TYPE_NONE;
        } else if (wifiState != null && State.CONNECTED == wifiState) {  
            Log.i("NetInfo", "当前网络类型:Wifi网络"); 
            return Constants.NETWORK_TYPE_WIFI;
        }
        else{
            Log.i("NetInfo", "当前网络类型:无网络"); 
            return Constants.NETWORK_TYPE_NONE;
        }
    }

    /**
     * 判断手机是否处于飞行模式.
     * 
     * @param context
     * @return
     */
    public static boolean isAirplaneModeOn(Context context) {
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) != 0;
    }

    /**
     * 获取设备序列号.
     * 
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getDeviceId();
    }

    /**
     * 判断手机SDCard是否已安装并可读写.
     * 
     * @return
     */
    public static boolean isSDCardUsable() {
        return Environment.MEDIA_MOUNTED.equalsIgnoreCase(Environment.getExternalStorageState());
    }

    /**
     * 获取指定的SDCard中图片缓存目录.
     * 
     * @param defaultImageFolderName
     * @return
     */
    public static File getImgCacheDir(String defaultImageFolderName) {
        if (isSDCardUsable()) {
            File dir = new File(Environment.getExternalStorageDirectory(), defaultImageFolderName);
            if (!dir.exists()) dir.mkdirs();

            return dir;
        }
        return null;
    }

    /**
     * 隐藏某焦点控件弹出的软件键盘.
     * 
     * @param context
     * @param view
     */
    public static void hideSoftKeyboardFromView(Context context, View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
        IBinder binder = view.getWindowToken();
        inputMethodManager.hideSoftInputFromWindow(binder, 0);
    }
}
